


import 'package:flutter/material.dart';
import 'package:moonplay/common/app_common.dart';
import 'package:moonplay/model/list_type_model.dart';

class ListTypePage extends StatefulWidget {
  final dynamic arguments;

  const ListTypePage({Key key, this.arguments}) : super(key: key);
  @override
  _ListTypePageState createState() => _ListTypePageState();
}

class _ListTypePageState extends State<ListTypePage> {
  Future _futureBuilderFuture;
  ListTypeModel listTypeModel;

  @override
  void initState() {
    super.initState();
    init();
  }
  @override
  Widget build(BuildContext context) {
    print(widget.arguments['title']);
    return Scaffold(
      appBar: commonAppbar(context, title: widget.arguments['title']),
      body: commonFutureBuilder(
        context,
        future: _futureBuilderFuture,
        callWidget: listTypeModel != null ? bodyWidget() : Container(),
        callTap: init
      ),
    );
  }

  Widget bodyWidget(){
    return GridView.builder(
      itemCount: listTypeModel.data.videoList.length,
      //SliverGridDelegateWithFixedCrossAxisCount 构建一个横轴固定数量Widget
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
        //横轴元素个数
        crossAxisCount: 3,
        //纵轴间距
        mainAxisSpacing: 3.0,
        //横轴间距
        crossAxisSpacing: 3.0,
        //子组件宽高长度比例
        childAspectRatio: 0.6
      ),
      itemBuilder: (BuildContext context, int idx) {
        return InkResponse(
          highlightColor: Colors.transparent,
          radius: 0,
          onTap: (){
            logger.d('${listTypeModel.data.videoList[idx].videoId}');
            // Get.toNamed('/VideoDetailPage', arguments: {"videoId": listTypeModel.data.videoList[idx].videoId});
          },
          child: Container(
            child: Stack(
              children: <Widget>[
                Container(
                  height: double.infinity,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.all(Radius.circular(4))
                  ),
                  child: Column(
                    children: <Widget>[
                      Container(
                        height: ScreenUtil.getInstance().screenWidth / 3 / 0.75,
                        child: Stack(
                          children: <Widget>[
                            Container(
                              height: ScreenUtil.getInstance().screenWidth / 3 / 0.75,
                              decoration: BoxDecoration(
                                image: DecorationImage(
                                  image: NetworkImage(listTypeModel.data.videoList[idx].imgUrl),
                                  fit: BoxFit.cover,
                                ),
                                borderRadius: BorderRadius.all(Radius.circular(4))
                              ),
                            ),
                            listTypeModel.data.videoList[idx].updateText != null 
                            ? Positioned(
                              bottom: 0,
                              left: 0,
                              right: 0,
                              child: Container(
                                height: ScreenUtil().getWidthPx(60),
                                alignment: Alignment.centerLeft,
                                padding: EdgeInsets.only(left: ScreenUtil().getWidthPx(10)),
                                color: Color.fromRGBO(0, 0, 0, 0.7),
                                child: Cptext(text: listTypeModel.data.videoList[idx].updateText, style: TextStyle(color: Colors.white, fontSize: 14),),
                              )
                            )
                            : Container()
                          ],
                        ),
                      ),
                      Expanded(
                        child: Container(
                          alignment: Alignment.center,
                          child: Cptext(text: listTypeModel.data.videoList[idx].title, style: TextStyle(fontSize: 14),),
                        ),
                      )
                    ],
                  ),
                ),
              ],
            )
          ),
        );
      }
    );
  }


  //网络初始化
  init(){
    setState(() {
      _futureBuilderFuture = initGet();
    });
  }

  initGet() async {
    String url = "http://api.tgmmvip.com/wolong/gae/video/album?albumId=${widget.arguments['albumId']}";
    var _res = await HttpUtil().request(url, method: 'get', need_login: false);
    logger.d(_res);
    setState(() {
      listTypeModel = ListTypeModel.fromJson(_res);
    });
    if(_res != null) {
      return _res;
    } else {
      return null;
    }
  }
}